import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    curList: {}, //当前播放歌曲
    allList: [], // 播放列表
    playflag: false, // 迷你播放窗口展示状态
    loginflag: false, // 登录状态
    love: [], //喜欢的音乐列表
    seaHistory: [], // 搜索历史
    collectList: [], // 收藏的歌单
  },
  mutations: {
    curListFn(state, data) {
      state.curList = data;
    },
    allListFn(state, data) {
      state.allList = data;
    },
    playflagFn(state, flag) {
      if (flag) {
        state.playflag = true;
      } else {
        state.playflag = false;
      }
    },
    setLogin(state, flag) {
      if (flag) {
        state.loginflag = true;
      } else {
        state.loginflag = false;
      }
    },
    loveFn(state) {
      let flag = true;
      if (state.love.length) {
        state.love.filter(item => {
          if (item.id == state.curList.id) {
            // console.log("点击了取消收藏");
            flag = false;
            return true;
          }
        })
      }
      if (flag) {
        state.love.push(state.curList);
      } else {
        // 再点取消
        state.love.filter((item, index) => {
          if (item.id == state.curList.id) {
            state.love.splice(index, 1);
            return true;
          }
        })
      }
    },
    seaHistoryFn(state, text) {
      let flag = state.seaHistory.some(item => {
        if (item == text) {
          return true;
        }
      })
      // 搜索历史保存
      if (state.seaHistory.length < 10) {
        if (!flag) {
          state.seaHistory.push(text);
        }
      } else {
        state.seaHistory.shift();
        if (!flag) {
          state.seaHistory.push(text);
        }
      }
    },
    clearHistoryFn(state) {
      state.seaHistory = [];
    },
    setcollectListFn(state, data) {
      let flagIndex = -1;
      state.collectList.some((item, index) => {
        if (item.id == data.id) {
          flagIndex = index;
          return true;
        }
      })
      if (flagIndex == -1) {
        state.collectList.push(data);
      } else {
        state.collectList.splice(flagIndex, 1);
      }
    },
  },
  actions: {},
  modules: {}
})